<?php
/**
* @package Frontend-User-Access (com_frontenduseraccess)
* @version 3.0.8
* @copyright Copyright (C) 2008 Carsten Engel. All rights reserved.
* @license GPL versions free/trial/pro
* @author http://www.pages-and-items.com
* @joomla Joomla is Free Software
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view');

class frontenduseraccessViewItems extends JView
{
	function display($tpl = null)
	{		
		$database = JFactory::getDBO();
		$controller = new frontenduseraccessController();	
		$this->assignRef('controller', $controller);	
		
		//toolbar			
		JToolBarHelper::custom( 'access_items_save', 'save.png', 'save_f2.png', JText::_('save'), false, false );		
		
		//get usergroups from db
		$fua_usergroups = $controller->get_usergroups();
		$this->assignRef('fua_usergroups', $fua_usergroups);	
		
		//get item access from db
		$controller->db->setQuery("SELECT itemid_groupid FROM #__fua_items");
		$access_items = $controller->db->loadResultArray();
		$this->assignRef('access_items', $access_items);	
		
		//get categories
		$controller->db->setQuery( "SELECT id, title, name "
		. "FROM #__categories "		
		. "ORDER BY title "		
		);		
		$categories = $controller->db->loadObjectList();
		$this->assignRef('categories', $categories);	
		
		//get sections
		$controller->db->setQuery( "SELECT id, title "
		. "FROM #__sections "		
		. "ORDER BY title"		
		);		
		$sections = $controller->db->loadObjectList();
		$this->assignRef('sections', $sections);				
		
		//get limits			
		global $mainframe, $option;
		$limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		$limitstart = $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );		
		$limit_query = "";
		if($limit!=0){
			$limit_query = "LIMIT ".$limitstart.", ".$limit;
		}	
		
		//do filter stuff
		$where = array();
		$where[] = "(state='-1' OR state='0' OR state='1') ";
		$search = $controller->get_var('search');
		if($search){
			$where[] = 'title LIKE '.$controller->db->Quote( '%'.$controller->db->getEscaped( $search, true ).'%', false ).' ';
		}	
		
		$category_filter = $controller->get_var('category_filter');
		if($category_filter){
			$where[] = '(catid = '.$category_filter.')';
		}
		
		$section_filter = $controller->get_var('section_filter');
		if($section_filter){
			$where[] = '(sectionid = '.$section_filter.')';
		}
				
		$where = ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );			
		
		//get items from db
		$controller->db->setQuery( "SELECT SQL_CALC_FOUND_ROWS id, title "
		. "\nFROM #__content "					
		. $where
		. "\nORDER BY title "
		. $limit_query
		);		
		$items = $controller->db->loadObjectList();	
		$this->assignRef('items', $items);		

		//get total from db just for pagination
		$controller->db->setQuery('SELECT FOUND_ROWS();');
		$total = $controller->db->loadResult();
		
		//get pagination stuff
		jimport( 'joomla.html.pagination' );
		$pagination = new JPagination($total,$limitstart,$limit);
		$this->assignRef('pagination', $pagination);
		
		$this->assignRef('search', $search);	
		$this->assignRef('category_filter', $category_filter);
		$this->assignRef('section_filter', $section_filter);

		parent::display($tpl);
	}

}
?>